<?php
namespace system\core;

/**
 * Benchmark
 *
 * @author Steffen Brem <steffenbrem@gmail.com>
 * @package System.core
 */
class Benchmark 
{
    private static $_groups = array();
    
    public static function start( $group = '' )
    {
        $time = microtime();
        $time = explode(' ', $time);
        $time = $time[1] + $time[0];
        
        self::$_groups[$group]['start'] = $time;
    }
    
    public static function stop( $group )
    {
        if ( ! key_exists($group, self::$_groups) )
        {
            throw new \Exception('The Benchmark group "' . $group / '" does not exist.');
        }
        $time = microtime();
        $time = explode(' ', $time);
        $time = $time[1] + $time[0];
        $finish = $time;
        $total_time = round(($finish - self::$_groups[$group]['start']), 3);
        self::$_groups[$group]['stop'] = $total_time;
    }
    
    public static function get( $group, $milisec = FALSE )
    {
        if ( ! key_exists($group, self::$_groups) )
        {
            throw new \Exception('The Benchmark group "' . $group . '" does not exist.');
        }
        if ( ! isset(self::$_groups[$group]['stop']) )
        {
            self::stop($group);
        }
        $total_time = self::$_groups[$group]['stop'];
        if ( $milisec === TRUE )
        {
            $total_time *= 1000;
        }
        return $total_time;
    }
}
